package com.longxi.tianjintianbao.ui.presenter.main;

import android.os.AsyncTask;
import android.os.SystemClock;
import android.widget.Toast;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.PgyCheck;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.main.IAboutAtView;
import com.longxi.tianjintianbao.utils.APKVersionCodeUtils;
import com.longxi.tianjintianbao.utils.MyUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AboutAtPresenter extends BasePresenter<IAboutAtView> {

    public AboutAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setVersion(){
        getView().getTvVersion().setText("当前版本:"+ APKVersionCodeUtils.getVerName(mContext));
    }

    public void check(){
        mContext.showWaitingDialog(MyUtils.getResource().getString(R.string.load));
        ApiRetrofit.getInstance().pgyCheck("")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<PgyCheck>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
            }

            @Override
            public void onNext(PgyCheck pgyCheck) {
                mContext.hideWaitingDialog();
                getView().getTvUpdateContent().setText(pgyCheck.getData().getBuildUpdateDescription());
            }
        });
    }

    public void startAsyncTask() {

        // This async task is an anonymous class and therefore has a hidden reference to the outer
        // class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),
        // the activity instance will leak.
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                // Do some slow work in background
                SystemClock.sleep(200000);
                return null;
            }
        }.execute();
        Toast.makeText(mContext, "请关闭这个A完成泄露", Toast.LENGTH_SHORT).show();
    }
}
